/*
 * @Author: xiaosihan 
 * @Date: 2022-10-18 10:54:05 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2022-11-07 15:26:57
 */


//mesh
import home2Store from "@views/Home2/home2Store";
import { autorun } from "mobx";
import { Color, DoubleSide, ShaderChunk, ShaderMaterial, MeshStandardMaterial } from "three";
import MeshBase from "three-base/MeshBase";
import threeUtils from "three-base/threeUtils";

// 人物模型 
export default class ManikinMesh extends MeshBase {
    material: MeshStandardMaterial | ShaderMaterial;
    constructor(url?: string) {
        super(url);
        this.material = this.material1;
        this.addEventListener("loaded", () => {
            this.traverseMesh((mesh) => {
                mesh.material = this.material1;
            });
        });
    }

    material1 = new MeshStandardMaterial({
        color: "#94B0BD",
        side: DoubleSide
    });

    material2 = new ShaderMaterial({
        uniforms: {
            // color: { value: new Color("#8DE4FD") }, // 浅蓝色
            color: { value: new Color("#12dcfa") }, // 浅绿色
            c: { value: 1.0 },
            p: { value: 0.5 },
        },
        vertexShader: `
            ${ShaderChunk.common}
            ${ShaderChunk.logdepthbuf_pars_vertex}
            uniform float c;
            uniform float p;
            varying vec2 vUv;
            uniform vec3 cameraPos;
            varying float intensity;
            void main() {
                vUv = uv;
                vec3 vNormal = normalize( normalMatrix * normal );
                vec3 vNormel = normalize( normalMatrix * (cameraPosition - position) );
                intensity = pow( c - dot(vNormal, vNormel), p );
                // gl_Position = vec4(position, 1.0);
                gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
                ${ShaderChunk.logdepthbuf_vertex}
            }
        `,
        fragmentShader: `
            ${ShaderChunk.logdepthbuf_pars_fragment}
            uniform vec3 color;
            varying vec2 vUv;
            varying float intensity;
            float opacity;
            void main() {
                // gl_FragColor = vec4(color, intensity);
                vec3 glow = color * intensity;
                gl_FragColor = vec4( glow, 1.0 );
                ${ShaderChunk.logdepthbuf_fragment}
            }
        `,
        side: DoubleSide,
        depthTest: true,
        depthWrite: true,
        transparent: true
    });

     //是否显示弹窗
  dispose = autorun(() => {
    const { value } = home2Store;
    this.traverseMesh((mesh) => {
        mesh.material =  [this.material1,this.material2][value];
    });
  });

}